package com.google.code.chaotic.server;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sun.enterprise.deployment.runtime.web.Servlet;

public final class EMF {
	private static final EntityManagerFactory instance = Persistence
			.createEntityManagerFactory("backendWebLocal");

	private EMF() {
		// new org.eclipse.emf.teneo.eclipselink.EmfSessionCustomizer();
	}

	public static EntityManagerFactory get() {
		return instance;
	}

	public static EntityManager getEntityManager(HttpServletRequest request) {
		HttpSession session = request.getSession();
		EntityManager em = (EntityManager) session
				.getAttribute("entityManager");
		if (em == null) {
			em = get().createEntityManager();
			// em = emf.createEntityManager();
			session.setAttribute("entityManager", em);
		}
		return em;
	}
}